import { Module } from 'vuex'
import { ISystemState } from './types'
import { IRootState } from '@/store/types'
import { getPageListData } from '@/service/main/system/system'
const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0
    }
  },
  getters: {
    getPageList(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
      }
    },
    getPageCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  mutations: {
    setUsersList(state, list: any[]) {
      state.usersList = list
    },
    setUsersCount(state, count: number) {
      state.usersCount = count
    },
    setRoleList(state, list: any[]) {
      state.roleList = list
    },
    setRoleCount(state, count: number) {
      state.roleCount = count
    },
    setGoodsList(state, list: any[]) {
      state.goodsList = list
    },
    setGoodsCount(state, count: number) {
      state.goodsCount = count
    },
    setMenuList(state, list: any[]) {
      state.menuList = list
    },
    setMenuCount(state, count: number) {
      state.menuCount = count
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      const pageUrl = `/${payload.pageName}/list`
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const { list, totalCount } = pageResult.data
      const name =
        (payload.pageName as string).slice(0, 1).toLocaleUpperCase() +
        (payload.pageName as string).slice(1)
      commit(`set${name}List`, list)
      commit(`set${name}Count`, totalCount)
    }
  }
}

export default systemModule
